package com.uwlaser.rest;

import com.uwlaser.annotation.BaseMethodTag;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.*;
import com.uwlaser.domain.RequestParam;
import com.uwlaser.service.*;
import com.uwlaser.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.UUID;
import java.util.stream.Collectors;


/**
* @author zk-Qin
* @date 2022-05-11
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "设备请求MES接口")
@RequestMapping("/api/deviceToMes")
public class DeviceToMesController {

    private final RequestParamService requestParamService;

    private final ProductService productService;

    private final CraftService craftService;

    private final PlanService planService;

    private final InboundService inboundService;

    private final OutboundService outboundService;

    private final DeviceService deviceService;

    private final DataMaterialSingleService dataMaterialSingleService;

    private final AntonService antonService;

    private final DataCellCapacityService dataCellCapacityService;
    /**
     * 追溯登陆接口
     * @param param
     * @return
     */
    @BaseMethodTag
    @PostMapping("/login")
    @Log("人员登陆接口")
    @ApiOperation("人员登陆接口")
    public ResultEntity login(@Validated @RequestBody DeviceLoginParam param){
        RequestParam<DeviceLoginParam> requestParam = requestParamService.getRequestParam(param);
        DeviceLoginResult result = deviceService.getDeviceLogin(requestParam);
        return new ResultEntity<>(result,"登陆成功");
    }


    /**
     * 进站接口（单个）
     * @param param
     * @return
     */
    @PostMapping("/inbound")
    @Log("产品进站检测接口-单个")
    @ApiOperation("产品进站检测接口-单个")
    public ResultEntity inbound (@Validated @RequestBody InboundParam param){
        RequestParam<InboundParam> requestParam = requestParamService.getRequestParam(param);
        return inboundService.inbound(requestParam);
    }

    /**
     * 进站接口 -多个
     * @param param
     * @return
     */
    @PostMapping("/inboundBatch")
    @Log("产品进站检测接口-多个")
    @ApiOperation("产品进站检测接口-多个")
    public ResultEntity inboundBatch (@Validated @RequestBody InboundBatchParam param){
        RequestParam<InboundBatchParam> requestParam = requestParamService.getRequestParam(param);
        return inboundService.inboundBatch(requestParam);
    }


    /**
     * 出站上传接口 -单个
     * @param param
     * @return
     */
    @PostMapping("/outbound")
    @Log("出站上传接口-单个")
    @ApiOperation("出站上传接口-单个")
    public ResultEntity outbound (@Validated @RequestBody OutboundParam param) {

        RequestParam<OutboundParam> requestParam = requestParamService.getRequestParam(param);
        return outboundService.outbound(requestParam);
    }

    /**
     * 出站上传接口 -多个
     * @param param
     * @return
     */
    @PostMapping("/outboundBatch")
    @Log("出站上传接口-多个")
    @ApiOperation("出站上传接口-多个")
    public ResultEntity outboundBatch (@Validated @RequestBody OutboundBatchParam param) {
        RequestParam<OutboundBatchParam> requestParam = requestParamService.getRequestParam(param);
        return outboundService.outboundBatch(requestParam);
    }

    /**
     * 获取工单信息
     * @param param
     * @return
     */
    @PostMapping("/getWorkOrder")
    @Log("获取工单信息")
    @ApiOperation("获取工单信息")
    public ResultEntity getWorkOrder (@Validated @RequestBody GetWorkOrderParam param){

        RequestParam<GetWorkOrderParam> requestParam = requestParamService.getRequestParam(param);

        GetWorkOrderResult result = planService.getWorkOrder(requestParam);

        return new ResultEntity<>(result,"获取工单成功");
    }

    /**
     * 获取临时产品序列号
     * @param param
     * @return
     */
    @PostMapping("/getTempProductSn")
    @Log("获取临时产品序列号")
    @ApiOperation("获取临时产品序列号")
    public ResultEntity getTempProductSn (@Validated @RequestBody GetProductSnParam param){

        RequestParam<GetProductSnParam> requestParam = requestParamService.getRequestParam(param);

        GetProductSnResult result = productService.getTempProductSn(requestParam);

        return new ResultEntity<>(result,"获取产品序列号成功");
    }

    /**
     * 获取产品序列号
     * @param param
     * @return
     */
    @PostMapping("/getProductSn")
    @Log("获取产品序列号")
    @ApiOperation("获取产品序列号")
    public ResultEntity getProductSn (@Validated @RequestBody GetProductSnParam param){

        RequestParam<GetProductSnParam> requestParam = requestParamService.getRequestParam(param);

        GetProductSnResult result = productService.getProductSn(requestParam);

        return new ResultEntity<>(result,"获取产品序列号成功");
    }

    /**
     * 添加产品序列号-单个
     * @param param
     * @return
     */
    @PostMapping("/addProductSn")
    @Log("添加产品序列号-单个")
    @ApiOperation("添加产品序列号-单个")
    public ResultEntity addProductSn (@Validated @RequestBody AddProductSnParam param){

        RequestParam<AddProductSnParam> requestParam = requestParamService.getRequestParam(param);

        AddProductSnResult result = productService.addProductSn(requestParam);

        return new ResultEntity<>(result,"添加产品序列号成功");
    }

    /**
     * 添加产品序列号-单个
     * @param param
     * @return
     */
    @PostMapping("/addProductSnBatch")
    @Log("添加产品序列号-多个")
    @ApiOperation("添加产品序列号-多个")
    public ResultEntity addProductSnBatch (@Validated @RequestBody AddProductSnBatchParam param){

        RequestParam<AddProductSnBatchParam> requestParam = requestParamService.getRequestParam(param);

        AddProductSnBatchResult result = productService.addProductSnBatch(requestParam);

        return new ResultEntity<>(result,"添加产品序列号成功");
    }

    /**
     * 替换临时编码
     * @param param
     * @return
     */
    @PostMapping("/replaceTempProductSn")
    @Log("替换临时编码")
    @ApiOperation("替换临时编码")
    public ResultEntity replaceTempProductSn (@Validated @RequestBody ReplaceTempProductSnParam param){

        RequestParam<ReplaceTempProductSnParam> requestParam = requestParamService.getRequestParam(param);

        productService.replaceTempProductSn(requestParam);

        return new ResultEntity<>(new Object(),"替换临时产品序列号成功");
    }

    /**
     * 获取配方信息
     * @param param
     * @return
     */
    @PostMapping("/getRecipeInfo")
    @Log("获取配方信息")
    @ApiOperation("获取配方信息")
    public ResultEntity getRecipeInfo (@Validated @RequestBody GetRecipeInfoParam param){

        RequestParam<GetRecipeInfoParam> requestParam = requestParamService.getRequestParam(param);

        GetRecipeInfoResult result = craftService.getRecipeInfo(requestParam);

        return new ResultEntity<>(result,"获取配方信息成功");
    }


    /**
     * 扫码校验-单个
     * @param param
     * @return
     */
    @PostMapping("/scanCodeCheck")
    @Log("扫码校验-单个")
    @ApiOperation("扫码校验-单个")
    public ResultEntity scanCodeCheck (@Validated @RequestBody ScanCodeCheckParam param){

        RequestParam<ScanCodeCheckParam> requestParam = requestParamService.getRequestParam(param);

        ScanCodeCheckResult result = craftService.scanCodeCheck(requestParam);

        return new ResultEntity<>(result,"扫码校验成功");
    }

    /**
     * 扫码校验-多个
     * @param param
     * @return
     */
    @PostMapping("/scanCodeCheckBatch")
    @Log("扫码校验-多个")
    @ApiOperation("扫码校验-多个")
    public ResultEntity scanCodeCheckBatch (@Validated @RequestBody ScanCodeCheckBatchParam param){

        RequestParam<ScanCodeCheckBatchParam> requestParam = requestParamService.getRequestParam(param);

        List<ScanCodeCheckBatchResult> result = craftService.scanCodeCheckBatch(requestParam);

        //过滤校验后的集合中是否存在校验失败的条码
        List<ScanCodeCheckBatchResult> filterList = result.stream().filter(rs -> rs.getStatus() == 400).collect(Collectors.toList());
        if(filterList.size()>0){
            List<String> messageList = filterList.stream().map(ScanCodeCheckBatchResult::getMessage).collect(Collectors.toList());
            //过滤后的集合大于0就说明存在校验不通过的物料,返回校验失败和校验信息
            return new ResultEntity(result,"扫码校验失败，校验信息："+messageList.toString(),400);
        }
        return new ResultEntity<>(result,"扫码校验成功");
    }

    /**
     * 设备停机记录
     * @param param
     * @return
     */
    @PostMapping("/deviceDown")
    @Log("设备停机记录")
    @ApiOperation("设备停机记录")
    public ResultEntity deviceDown (@Validated @RequestBody DeviceDownParam param){

        RequestParam<DeviceDownParam> requestParam = requestParamService.getRequestParam(param);

        deviceService.deviceDown(requestParam);

        return new ResultEntity<>(new Object(),"设备停机记录成功");
    }

    /**
     * 设备报警记录
     * @param param
     * @return
     */
    @PostMapping("/deviceAlarm")
    @Log("设备报警记录")
    @ApiOperation("设备报警记录")
    public ResultEntity deviceAlarm (@Validated @RequestBody DeviceAlarmParam param){

        RequestParam<DeviceAlarmParam> requestParam = requestParamService.getRequestParam(param);

        deviceService.deviceAlarm(requestParam);

        return new ResultEntity<>(new Object(),"设备报警记录成功");


    }

    /**
     * 设备状态记录
     * @param param
     * @return
     */
    @PostMapping("/deviceStatus")
    @Log("设备状态记录")
    @ApiOperation("设备状态记录")
    public ResultEntity deviceStatus (@Validated @RequestBody DeviceStatusParam param){

        RequestParam<DeviceStatusParam> requestParam = requestParamService.getRequestParam(param);

        deviceService.deviceStatus(requestParam);

        return new ResultEntity<>(new Object(),"设备状态记录成功");
    }

    /**
     * 设备心跳
     * @param param
     * @return
     */
    @PostMapping("/deviceHeartBeat")
    //@Log("设备心跳")
    @ApiOperation("设备心跳")
    public ResultEntity deviceHeartBeat (@Validated @RequestBody DeviceHeartBeatParam param){

        RequestParam<DeviceHeartBeatParam> requestParam = requestParamService.getRequestParam(param);

        deviceService.deviceHeartBeat(requestParam);

        return new ResultEntity<>(new Object(),"设备心跳记录成功");
    }

    /**
     * 根据电芯码获取模组码
     * @param param
     * @return
     */
    @PostMapping("/getProductSnByCell")
    @Log("根据电芯码获取模组码")
    @ApiOperation("根据电芯码获取模组码")
    public ResultEntity getProductSnByCell (@Validated @RequestBody GetProductSnByCellBatchParam param){

        DataRequestParam<GetProductSnByCellBatchParam> requestParam = requestParamService.getDataRequestParam(param);

        DataMaterialSingle productSnByCell = dataMaterialSingleService.getProductSnByCell(requestParam);

        return new ResultEntity<>(productSnByCell,"获取模组码成功！");
    }

    /**
     * 根据托盘码获取模组码
     * @param param
     * @return
     */
    @PostMapping("/getProductSnByTrayCode")
    @Log("根据托盘码获取模组码")
    @ApiOperation("根据托盘码获取模组码")
    public ResultEntity getProductSnByTrayCode (@Validated @RequestBody GetProductSnByTrayCodeParam param){

        DataRequestParam<GetProductSnByTrayCodeParam> requestParam = requestParamService.getDataRequestParam(param);

        List<DataMainDto> productSnByTrayList = dataMaterialSingleService.getProductSnByTray(requestParam);

        return new ResultEntity<>(productSnByTrayList,"获取模组码成功！");
    }

    /**
     * 获取服务器时间
     * @param param
     * @return
     */
    @PostMapping("/getServerDateTime")
    @Log("获取服务器时间")
    @ApiOperation("获取服务器时间")
    public ResultEntity getServerDateTime(@Validated @RequestBody BaseParam param){

        return new ResultEntity(new Object(),"获取服务器时间成功！");
    }

    /**
     * 根据箱体码获取模组码
     * @param param
     * @return
     */
    @PostMapping("/getPackSnByTrayCode")
    @Log("根据箱体码获取PACK码")
    @ApiOperation("根据箱体码获取PACK码")
    public ResultEntity getPackSnByBoxCode (@Validated @RequestBody GetPackSnByBoxCodeParam param) {

        DataRequestParam<GetPackSnByBoxCodeParam> requestParam = requestParamService.getDataRequestParam(param);

        DataMainDto packSnByTrayCode = dataMaterialSingleService.getPackSnByTrayCode(requestParam);

        return new ResultEntity<>(packSnByTrayCode, "获取Pack码成功！");
    }
    /**
     * 根据设备编号获取安东配置信息
     * @return
     */
    @PostMapping("/getAntonConfigByDeviceCode")
    @Log("获取设备编号对应得安东配置信息")
    @ApiOperation("获取设备编号对应得安东配置信息")
    public ResultEntity getAntonConfigByDeviceCode(@Validated @RequestBody BaseParam param){
//        RequestParam<BaseParam> requestParam = requestParamService.getRequestParam(param);

        List<AntonConfigResult> antonConfigResults= antonService.getAntonConfigByDeviceCode(param);
        AntonConfigBantch antonConfigBantch =new AntonConfigBantch();
        antonConfigBantch.setDeviceCode(param.deviceCode);
        antonConfigBantch.setAntonConfigResultList(antonConfigResults);
        return new ResultEntity<>(antonConfigBantch,"根据设备编号获取安东配置信息成功");
    }

    /**
     * 获取所有安东配置信息
     * @return
     */
    @PostMapping("/getAllAntonConfig")
    @Log("获取所有安东配置信息")
    @ApiOperation("获取所有安东配置信息")
    public ResultEntity getAllAntonConfig(){
        List<AntonConfigBantch> result =antonService.getAllAntonConfig();
        return new ResultEntity<>(result,"获取所有安东配置信息成功");
    }

    /**
     * 上传安东记录
     * @return
     */
    @PostMapping("/uploadAntonInfo")
    @Log("上传安东记录")
    @ApiOperation("上传安东记录")
    public ResultEntity uploadAntonInfo(@Validated @RequestBody AntonRecordProductParam param){
        return antonService.uploadAntonInfo(param);
    }

    /**
     * 根据箱体码获取模组码
     * @param param
     * @return
     */
    @PostMapping("/unbindingTray")
    @Log("托盘解绑")
    @ApiOperation("托盘解绑")
    public ResultEntity unbindingTray(@Validated @RequestBody GetPackSnByBoxCodeParam param){

        DataRequestParam<GetPackSnByBoxCodeParam> requestParam = requestParamService.getDataRequestParam(param);

        int i = dataMaterialSingleService.unbindingTray(requestParam);
        if(i>0){
            return new ResultEntity<>(i,"解绑成功!");
        }else {
            return new ResultEntity<>(i,"解绑失败！",400);
        }
    }

    /**
     * 根据电芯码获取容量
     * @param param
     * @return
     */
    @PostMapping("/getCapacityByProductSn")
    @Log("根据电芯码获取容量")
    @ApiOperation("根据电芯码获取容量")
    public ResultEntity getCapacityByProductSn (@Validated @RequestBody GetCapacityByProductSnParam param) {

        DataCellCapacityDto capacityByProductSn = dataCellCapacityService.getCapacityByProductSn(param);

        return new ResultEntity<>(capacityByProductSn, "获取容量成功！");
    }


    /**
     * 校验电芯档位
     * @param param
     * @return
     */
    /*@PostMapping("/checkGear")
    @Log("校验电芯档位")
    @ApiOperation("校验电芯档位")
    public ResultEntity checkGear (@Validated @RequestBody GetCapacityByProductSnParam param) {
        return new ResultEntity<>(dataCellCapacityService.checkGear(param), "档位校验成功！");
    }*/
}
